package com.zbkj.admin.controller;

import com.zbkj.common.model.course.CourseVideo;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.CourseVideoRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.vo.CourseVideoVo;
import com.zbkj.service.service.CourseVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程视频控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/course/video")
@Api(tags = "课程管理-视频管理")
public class CourseVideoController {

    @Autowired
    private CourseVideoService courseVideoService;

    /**
     * 分页列表
     * @param courseId 课程ID
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<CourseVideo>> getList(
            @RequestParam(value = "courseId") Integer courseId,
            @Validated PageParamRequest pageParamRequest) {
        List<CourseVideo> courseVideoList = courseVideoService.getList(courseId, pageParamRequest);
        return CommonResult.success(CommonPage.restPage(courseVideoList));
    }

    /**
     * 保存课程视频
     * @param request 课程视频请求对象
     */
    @ApiOperation(value = "保存课程视频")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated CourseVideoRequest request) {
        CourseVideo courseVideo = new CourseVideo();
        BeanUtils.copyProperties(request, courseVideo);

        if (courseVideoService.saveVideo(courseVideo)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 更新课程视频
     * @param request 课程视频请求对象
     */
    @ApiOperation(value = "更新课程视频")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated CourseVideoRequest request) {
        if (request.getId() == null) {
            return CommonResult.validateFailed("视频ID不能为空");
        }

        CourseVideo courseVideo = new CourseVideo();
        BeanUtils.copyProperties(request, courseVideo);

        if (courseVideoService.updateVideo(courseVideo)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除课程视频
     * @param id 视频ID
     */
    @ApiOperation(value = "删除课程视频")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (courseVideoService.deleteVideo(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据课程ID获取视频列表（前端API）
     * @param courseId 课程ID
     */
    @ApiOperation(value = "获取课程视频列表")
    @RequestMapping(value = "/course/{courseId}", method = RequestMethod.GET)
    public CommonResult<List<CourseVideoVo>> getByCourseId(@PathVariable(value = "courseId") Integer courseId) {
        return CommonResult.success(courseVideoService.getByCourseId(courseId));
    }
}